//
//  ChartSwiftView.swift
//  ChartsUnderstandAndUsage
//
//  Created by 张海峰 on 2018/9/17.
//  Copyright © 2018年 张海峰. All rights reserved.
//
//Charts框架地址
//https://github.com/danielgindi/Charts.git
//该demo地址
//https://github.com/FighterLightning/ChartsUnderstandAndUsage.git
/*折线图*/
import UIKit
import Charts

@objcMembers
class ChartSwiftView: UIView {
    
    let imgSize = CGSize(width: 20.0, height: 20.0)
    
    var circleColors :[UIColor] = [UIColor]()
    var lineChartView: LineChartView  = LineChartView()
    
    public var days: [String]?
    public var moods: [String]?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        
        //添加折线
        addLineChart()
        //折线图描述文字和样式
        chartDescription()
        //设置交互样式
        interactionStyle()
        //修改背景色和边框样式
        setBackgroundBorder()
        //设置x轴的样式属性
        setXAxisStyle()
        //设置y轴的样式属性
        setYAxisStyle()
        
        updataData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
//MARK:- UI和折线图基本样式
extension ChartSwiftView{
    //添加折线
    func addLineChart(){
        lineChartView.backgroundColor = .clear
        lineChartView.frame = bounds
        //设置左右边距
        lineChartView.setExtraOffsets(left: -15.0, top: -5.0, right: 0, bottom: 0)
        lineChartView.setVisibleXRangeMaximum(7)
        self.addSubview(lineChartView)
    }
    //设置交互样式
    func interactionStyle(){
        lineChartView.scaleYEnabled = false //取消Y轴缩放
        lineChartView.doubleTapToZoomEnabled = false //双击缩放
        lineChartView.dragEnabled = false //启用拖动手势
        lineChartView.dragDecelerationEnabled = false //拖拽后是否有惯性效果
    }
    //描述文字
    func chartDescription(){
        lineChartView.legend.formSize = 10.0 //（图例大小）默认是8
        lineChartView.legend.form = .none//图例头部样式
    }
    //修改背景色和边框样式
    func setBackgroundBorder(){
        lineChartView.drawBordersEnabled = true  //绘制图形区域边框
        lineChartView.borderColor = .hexColor(hex: "#D4D4D4")
        lineChartView.borderLineWidth = 0.5
    }
    //设置x轴的样式属性
    func setXAxisStyle(){
        //轴线宽、颜色、刻度、间隔
        lineChartView.xAxis.axisLineWidth = 0.5 //x轴宽度
        lineChartView.xAxis.axisLineColor = .hexColor(hex: "#D4D4D4")
        lineChartView.xAxis.valueFormatter = self
        //文字属性
        lineChartView.xAxis.labelPosition = .bottom
        lineChartView.xAxis.labelTextColor = .black
        lineChartView.xAxis.labelFont = .systemFont(ofSize: 11)
        lineChartView.xAxis.xOffset = 200.0
        
        //文字格式
        let formatter = NumberFormatter()
        lineChartView.xAxis.valueFormatter = DefaultAxisValueFormatter(formatter: formatter)
        lineChartView.xAxis.drawGridLinesEnabled = true
        lineChartView.xAxis.gridColor = .hexColor(hex: "#D4D4D4")
        lineChartView.xAxis.gridLineDashLengths = [10.0, 5.0]
        
    }
    
    //设置y轴的样式属性(分左、右侧)
    func setYAxisStyle(){
        //右侧(默认显示)
        
        lineChartView.leftAxis.axisMaximum = 19
        lineChartView.leftAxis.axisMinimum = 1
        lineChartView.leftAxis.axisLineWidth = 0.5
        lineChartView.leftAxis.drawLabelsEnabled = true
        lineChartView.leftAxis.axisLineColor = .hexColor(hex: "#D4D4D4")
        lineChartView.leftAxis.drawAxisLineEnabled = true
        lineChartView.leftAxis.enabled = true
        lineChartView.leftAxis.xOffset = 15.0
        lineChartView.leftAxis.labelCount = 10
        
        //0刻度线
        lineChartView.leftAxis.drawZeroLineEnabled = false //绘制0刻度线
        lineChartView.leftAxis.gridColor = .hexColor(hex: "#D4D4D4") //x轴对应网格线的颜色
        //分割线是虚线
        lineChartView.leftAxis.gridLineDashLengths = [10.0, 5.0]
        lineChartView.leftAxis.gridLineWidth = 0.4 //x轴对应网格线的大小
        lineChartView.leftAxis.labelTextColor = .black
        lineChartView.leftAxis.labelFont = .systemFont(ofSize: 11) //刻度文字大小
        
        lineChartView.rightAxis.enabled = false //禁用右侧的Y轴
    }
    
}

extension ChartSwiftView{
    
    func updataData(){
        
        var dataEntries = [ChartDataEntry]()
        
        guard let days = days else { return }
        guard let moods = moods else { return }
        
        for i in 0..<days.count {
            
            var y = 0
            
            switch moods[i] {
            case "开心":
                y = 18
            case "惊喜":
                y = 17
            case "得意":
                y = 16
            case "甜蜜":
                y = 15
            case "暖心":
                y = 14
            case "充实":
                y = 13
            case "平静":
                y = 10
            case "无聊":
                y = 12
            case "尴尬":
                y = 9
            case "不安":
                y = 5
            case "伤心":
                y = 2
            case "生气":
                y = 3
            case "流泪":
                y = 1
            case "裂开":
                y = 11
            case "疲惫":
                y = 4
            case "孤独":
                y = 6
            case "梦境":
                y = 7
            case "不知道":
                y = 8
            default:
                break
            }
            
            let day: String = days[i]
            let x: Double = Double(day) ?? 2.0
            
            var iconImg: UIImage = UIImage(named: moods[i]) ?? UIImage()
            iconImg = iconImg.scaleImg(to: imgSize)
            
            let entry = ChartDataEntry.init(x: x, y: Double(y), icon: iconImg)
            dataEntries.append(entry)
        }
        
        let scaleX: ViewPortHandler = lineChartView.viewPortHandler
        scaleX.setMinimumScaleX(1)
        
        //设置折线
        let chartDataSet = LineChartDataSet(entries: dataEntries, label: "")
        chartDataSet.setColor(.hexColor(hex: "#D4D4D4"))
        chartDataSet.lineWidth = 1 //线条宽度
        chartDataSet.drawCirclesEnabled = false
        chartDataSet.drawValuesEnabled = false
        chartDataSet.highlightEnabled = false
        
        let chartData = LineChartData(dataSet: chartDataSet)
        lineChartView.data = chartData
        //        lineChartView.animate(xAxisDuration: 2)//展示方式xAxisDuration 和 yAxisDuration两种
        lineChartView.xAxis.valueFormatter = self
        lineChartView.moveViewToX(Double(dataEntries.count))
    }
}

extension ChartSwiftView: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        
        switch Int(value) {
        case 1: return "周一"
        case 2: return "周二"
        case 3: return "周三"
        case 4: return "周四"
        case 5: return "周五"
        case 6: return "周六"
        case 7: return "周日"
        default:  return ""
        }
        
    }
    
}
